﻿using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Xml.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Science_And_Industry
{
    public partial class MainForm : Form
    {
        private DataRow[] RRR;
        private db_eve_DataSet.invMarketGroupsDataTable invMarketGroupsTable;
        private db_eve_DataSet.invTypesDataTable invTypesTableTable;

        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {            
/*
            WebRequest request = WebRequest.Create("http://api.eve-central.com/api/quicklook?typeid=34&usesystem=30000142&sethours=12");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.
            Console.WriteLine(response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            richTextBox1.Text = responseFromServer;
                // Display the content.
            //Console.WriteLine(responseFromServer);
            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();*/
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db_eve_DataSet.invTypes' table. You can move, or remove it, as needed.
            this.invTypesTableAdapter.Fill(this.db_eve_DataSet.invTypes);
            // TODO: This line of code loads data into the 'db_eve_DataSet.invMarketGroups' table. You can move, or remove it, as needed.
            this.invMarketGroupsTableAdapter.Fill(this.db_eve_DataSet.invMarketGroups);

            invMarketGroupsTable = invMarketGroupsTableAdapter.GetData();
            invTypesTableTable = invTypesTableAdapter.GetData();


            CreateTreeNodes();
        }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void button2_Click(object sender, EventArgs e)
        {
            CreateTreeNodes();
        }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void CreateTreeNodes()
        {
            db_eve_DataSet.invMarketGroupsDataTable A = invMarketGroupsTableAdapter.GetData();

            var pos = from n in A
                      where n.IsparentGroupIDNull()
                      //where n.marketGroupID != 2
                      where n.marketGroupID != 150
                      where n.marketGroupID != 19
                      where n.marketGroupID != 24
                      where n.marketGroupID != 1396
                      select n;

            treeView1.Nodes.Clear();
            treeView1.BeginUpdate();
            foreach (db_eve_DataSet.invMarketGroupsRow row in pos)
            {
                TreeNodeCollection TNC = treeView1.Nodes;
                TNC.Add(row.marketGroupID.ToString(), row.marketGroupName + " : " + row.marketGroupID.ToString());
                List<int> Result = new List<int>();
                TNC[TNC.Count - 1].Tag = Result;
                GetAllChieldinTree(invMarketGroupsTable, row.marketGroupID, ref Result);
                AddNode(A, row.marketGroupID, TNC[TNC.Count - 1].Nodes);
            }
            treeView1.EndUpdate();
        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public int AddNode(db_eve_DataSet.invMarketGroupsDataTable Table, int parent, TreeNodeCollection nodes)
        {
            var pos = from n in Table
                       where !n.IsparentGroupIDNull()
                       where !n.marketGroupName.Contains("Faction")
                       where n.parentGroupID == parent
                       select n;
            
            foreach (db_eve_DataSet.invMarketGroupsRow row in pos)
            {
                nodes.Add(row.marketGroupID.ToString(), row.marketGroupName + " : " + row.marketGroupID.ToString());
                nodes[nodes.Count - 1].Tag = (int)row.marketGroupID;
                List<int> Result = new List<int>();
                GetAllChieldinTree(invMarketGroupsTable, row.marketGroupID, ref Result);
                

                if (row.hasTypes == 0)
                {
                    AddNode(Table, row.marketGroupID, nodes[nodes.Count - 1].Nodes);
                }
                else
                {
                    Result.Add(row.marketGroupID);
                }
                nodes[nodes.Count - 1].Tag = Result;
            }            
            return 0;
        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            richTextBox1.Text = e.Node.Tag.ToString();
            string filter = "";
            List<int> Result = (List<int>)e.Node.Tag;

            filter = "MarketGroupID in (";
            for (int i = 0; i < Result.Count; i++)
            {
                filter += Result[i].ToString();
                if (Result.Count!=i+1)
                {
                    filter += " , ";
                }
            }
            filter += ")";

            RRR = invTypesTableTable.Select(filter);
            dataGridView1.DataSource = RRR;
        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void button2_Click_1(object sender, EventArgs e)
        {
            this.invTypesTableAdapter.FillByMarketGroup(this.db_eve_DataSet.invTypes, 109);
            
        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void button1_Click_1(object sender, EventArgs e)
        {

        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void GetAllChieldinTree(db_eve_DataSet.invMarketGroupsDataTable Table, int root, ref List<int> result)
        {            
            var pos = from n in Table
                      where !n.IsparentGroupIDNull()
                      where !n.marketGroupName.Contains("Faction")
                      where n.parentGroupID == root
                      select n;

            foreach (db_eve_DataSet.invMarketGroupsRow row in pos)
            {
                if (row.hasTypes == 0)
                {
                    GetAllChieldinTree(Table, row.marketGroupID, ref result);
                }
                else
                {
                    result.Add(row.marketGroupID);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //
            XDocument Doc = XDocument.Load(@"D:\Projects\SAI\Science_And_Industry\bin\Debug\AssetsCorp.xml");

            foreach (XNode node in Doc.Descendants())
            {
                int x = 0;
            }

            
        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
}